package com.project.controller;

import com.project.bean.UserBean;
import com.project.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("newuser")
public class UserController {
    @Autowired
    private IUserService service;

    @RequestMapping("login")
    public String login(String userCode, String name, String pwd, HttpServletRequest req) {
        UserBean user = service.login(name, pwd);
        if (user == null) {
            return "no";
        }
        String code = req.getSession().getAttribute("checkCode").toString();
        if (code.equalsIgnoreCase(userCode) == false) {
            return "nono";
        }
        req.getSession().setAttribute("loginUser", user);

        return "yes";
    }

    @RequestMapping("checkUser")
    public String checkUser(HttpServletRequest req) {
        UserBean user = (UserBean) req.getSession().getAttribute("loginUser");
        if (user != null) {
            return "欢迎光临，" + user.getName();
        }
        return "请先登录";
    }


}